package com.x.reader.service;

import com.x.framework.exception.BusinessException;
import com.x.framework.exception.ExceptionCode;
import com.x.framework.model.BaseModel;
import com.x.framework.service.BaseService;
import com.x.reader.dao.*;
import com.x.reader.model.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

@Component
public class CategoryService extends BaseService {

    @Autowired
    private CategoryDao categoryDao;

    @Autowired
    private BookDao bookDao;

    @Autowired
    private VideoDao videoDao;

    @Autowired
    private AudioDao audioDao;

    @Autowired
    private PaperDao paperDao;

    public List<Category> selectCategoryList(Category category) {
        try {
            return categoryDao.selectCategoryList(category);
        } catch (Exception e) {
            throw new BusinessException(ExceptionCode.EXCEPTION_CODE0000, e);
        }
    }

    public List<Category> selectCategoryDataList(Category category) {
        try {
            int categoryParentId = category.getCategoryParentId();
            int categoryId;
            List<Category> categoryList = categoryDao.selectCategoryList(category);
            for (Category c : categoryList) {
                categoryId = c.getCategoryId();
                List list = new ArrayList();
                if (categoryParentId == 1) {
                    Book book = new Book();
                    book.setCategoryId(categoryId);
                    book.setPageIndex(1);
                    book.setPageSize(3);
                    list = bookDao.selectBookList(book);
                } else if (categoryParentId == 2) {
                    Video video = new Video();
                    video.setCategoryId(categoryId);
                    video.setPageIndex(1);
                    video.setPageSize(3);
                    list = videoDao.selectVideoList(video);
                } else if (categoryParentId == 19) {
                    Audio audio = new Audio();
                    audio.setCategoryId(categoryId);
                    audio.setPageIndex(1);
                    audio.setPageSize(3);
                    list = audioDao.selectAudioList(audio);
                } else if (categoryParentId == 20) {
                    Paper paper = new Paper();
                    paper.setCategoryId(categoryId);
                    paper.setPageIndex(1);
                    paper.setPageSize(3);
                    list = paperDao.selectPaperList(paper);
                }
                c.setList(list);
            }
            return categoryList;
        } catch (Exception e) {
            throw new BusinessException(ExceptionCode.EXCEPTION_CODE0000, e);
        }
    }
}
